package com.futong.cloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.futong.cloud.action.ActionException;
import com.futong.cloud.action.ActionFactory;
import com.futong.cloud.action.ActionRegister;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @program: futong-cm
 * @description: 资源管理
 * @author: lpp
 * @create: 2021-10-11 15:14
 */
@Slf4j
@Api(tags = "资源管理")
@RequestMapping
@RestController
public class ActionController {
    @PostMapping("/action")
    @ApiOperation(value = "所有请求操作入口", notes = "所有请求操作入口")
    public JSONObject action(@RequestBody JSONObject param) {
        try {
            return ActionFactory.handler(param);
        }catch (ActionException e){
            log.error("action param info:{}",param);
            log.error("action error:",e);
            return ActionFactory.fail(e.getMessage(),e.code);
        }catch (Exception e){
            log.error("action param info:{}",param);
            log.error("action error:",e);
            return ActionFactory.fail(e.getMessage());
        }
    }
    @PostMapping("/refresh")
    @ApiOperation(value = "刷新action", notes = "刷新action")
    public JSONObject refresh() {
        try {
            ActionRegister.refresh();
            return ActionFactory.success();
        }catch (Exception e){
            log.error("action refresh error:",e);
            return ActionFactory.fail(e.getMessage());
        }
    }

}
